/* 用户注册 */
const router = require("koa-router")();
import { ReadFile, WriteFile } from "../utils/ReadWriteFile";
import { createId } from "../utils/createId";
function getCurrentFormattedTime() {
  const now = new Date();
  const year = now.getFullYear();
  const month = String(now.getMonth() + 1).padStart(2, "0"); // 月份是从0开始的，所以要加1
  const day = String(now.getDate()).padStart(2, "0");
  const hours = String(now.getHours()).padStart(2, "0");
  const minutes = String(now.getMinutes()).padStart(2, "0");
  const seconds = String(now.getSeconds()).padStart(2, "0");
  return `${year}-${month}-${day} ${hours}:${minutes}:${seconds}`;
}

router.post("/api/addroadCondition", async (ctx) => {
  const { username, password, type,road, content,coordinate } =
    ctx.request.body;


  // 读取用户信息
  let userResult = await ReadFile(type);
  const res = userResult.find((item) => item.username == username);

  // 读取路况文件
  let roadCondition = 'roadCondition'
  let roadConditionobjResult = await ReadFile(roadCondition);
  let id = createId();
  if (username) {
    if (road && content) {
      // 不用审核
      if(type == 'traffic'){
        let readdRes = roadConditionobjResult.filter((item) =>{
          return item.road !=road || item.content != content
        })
        // 更新roadcodition文件
        let roadConditionobj = {
          id,
          time: getCurrentFormattedTime(),
          road,
          content,
          coordinate,
          publisher: username,
          type,
          checked:true,
        };
        readdRes.unshift(roadConditionobj);
        WriteFile({
          type: roadCondition,
          result:readdRes,
        });
        // 给当前用户添加记录
        let res1 = userResult.filter((item) => item.username != username);
        let newRoadCondition = {
          id,
          time: getCurrentFormattedTime(),
          road,
          content,
          coordinate,
        }
        res.roadCondition.push(newRoadCondition)
        var obj = {
          id: createId(),
          username,
          password,
          type,
          interestPoints: res.interestPoints,
          announcenment: res.announcenment,
          roadCondition:res.roadCondition,
          isOnline: true,
        };
        res1.push(obj);
        WriteFile({
          type,
          result: res1,
        });
        ctx.body = {
          code: 200,
          msg: "发布成功",
          roadConditionobjResult,
          roadConditionobjResult,
          roadCondition
        };
      }
      // 需要审核
      if(type == 'common'){
        // 更新roadcodition文件
        let roadConditionobj = {
          id,
          time: getCurrentFormattedTime(),
          road,
          content,
          coordinate,
          publisher: username,
          type,
          checked:false,
        };
        roadConditionobjResult.unshift(roadConditionobj);
        WriteFile({
          type: roadCondition,
          result:roadConditionobjResult,
        });
        // 给当前用户添加记录
        let res1 = userResult.filter((item) => item.username != username);
        let newRoadCondition = {
          id,
          time: getCurrentFormattedTime(),
          road,
          content,
          coordinate,
        }
        res.roadCondition.push(newRoadCondition)
        var obj = {
          id: createId(),
          username,
          password,
          type,
          interestPoints: res.interestPoints,
          roadCondition:res.roadCondition,
          isOnline: true,
        };
        res1.push(obj);
        WriteFile({
          type,
          result: res1,
        });
        ctx.body = {
          code: 200,
          msg: "更新成功",
        };
      }
    } else {
      ctx.body = {
        code: 400,
        msg: "请输入有效内容",
      };
    }
  } else {
    ctx.body = {
      code: 400,
      msg: "请先登录",
    };
  }
});
module.exports = router;
